#1.ADdata
windows()
setwd("C:/Users/86183/Desktop/data for class3") #设置新路径
ADdata=read.csv("myresult.csv")
ADdata=ADdata[,-(1:2)]
ADdata1=unlist(ADdata)
ADdata2=ADdata1[ADdata1!=0]
ADdata3=log10(ADdata2)    #取对数
chart1=hist(ADdata3,label=T,col = topo.colors(10),
            main = "Histogram of ADdata",xlab="log10(ADdata)")

#1.GSE
library(openxlsx)
setwd("C:/Users/86183/Desktop/data for class3/GSE67835")
GSE=read.xlsx("10-张晗-GSE.xlsx")
GSE=GSE[,-(1:4)]
GSE1=unlist(GSE)
GSE2=GSE1[!(GSE1==0)]
GSE3=log10(GSE2)    #取对数
chart1=hist(GSE3,label=T, main = "Histogram of GSE" ,
            xlab="log10(GSE)",col= c("green","lightblue","pink","orange"))


#2.火山图
install.packages("ggplot2")
install.packages("ggrepel")
library(ggplot2)
library(ggrepel)
windows()
setwd("C:/Users/86183/Desktop/data for class3")
load("C:/Users/86183/Desktop/data for class3/class5_volcano.RData")
foldchange=2^prostat$FC
prostat$type=ifelse(prostat$P<0.05 & foldchange>1.2,"up",
                    ifelse(prostat$P<0.05 &foldchange<1/1.2,"down","normal"))
#ifelse的嵌套表明是否差异表达
table(prostat$type)
#查看上调，下调和正常个数
prostat$ID2=ifelse(prostat$type!="normal",prostat$ID,"")
jpeg("zh-火山图.jpg") #保存
p=ggplot(prostat,aes(x=FC,y=-log10(P)))+
  geom_point(aes(color=prostat$type))+  #这里是把散点图映射到画布上
  labs(title="volcanoplot",x="log2(FoldChange)",y="-log10P")+ #修改X轴名称
  scale_color_manual(values=c('up'="red",'normal'="gray",'down'="green"))+
  geom_vline(xintercept = c(-log2(1.2),log2(1.2)))+
  #加两条竖线
  geom_hline(yintercept =-log10 (0.05),lty=3,col="black",lwd=1)+
  #加条横线
  geom_text_repel(aes(label=prostat$ID2),color="black",size=3)
#添加基因标签
p
dev.off()


#3.层次聚类，树状图，plot函数画图
#分析病人信息差异
setwd("C:/Users/86183/Desktop/data for class3") #设置新路径
windows()
ADdata=read.csv("myresult.csv")
ADdata=ADdata[,-(1:2)]
distance=dist(t(ADdata))#dist函数求每一行间的距离
tree=hclust(distance,method="average")#hclust函数根据距离分类
plot(tree,hang=-1,cex=.8)#下方对齐，点的大小为cex.8

#3.分析蛋白差异
setwd("C:/Users/86183/Desktop/data for class3") #设置新路径
windows()
ADdata=read.csv("myresult.csv")
rownames(ADdata)<-ADdata[,2]#设置行名
ADdata=ADdata[,-(1:2)]
distance=dist(ADdata)
tree=hclust(distance,method="average")#hclust函数根据距离分类
plot(tree,hang=-1,cex=.8)#下方对齐，点的大小为cex.8
